#include "napi/native_api.h"
#include <iostream>
#include "./include/utils/StringUtils.h"
#include "utils/Base64Decoder.h"
#include "utils/Base64Encoder.h"
using namespace std;

static napi_value NAPI_String_isEmpty(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    if (argc < 1) {
        napi_throw_type_error(env, nullptr, "Expected one argument");
        return nullptr;
    }
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    size_t bufsize = 1024;
    char buffer[1024];
    size_t resultSize;
    napi_status status = napi_get_value_string_utf8(env, args[0], buffer, bufsize, &resultSize);
    if (status != napi_ok) {
        napi_throw_type_error(env, nullptr, "Argument must be a string");
        return nullptr;
    }
    std::string value(buffer, resultSize);
    bool isEmptyResult = StringUtils::isEmpty(value);
    napi_value result;
    napi_get_boolean(env, isEmptyResult, &result);
    return result;
}
static napi_value NAPI_Global_decoder(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (argc < 1) {
        napi_throw_type_error(env, nullptr, "Expected one argument");
        return nullptr;
    }
    size_t bufsize = 1024;
    char buffer[1024];
    size_t resultSize;
    napi_status status = napi_get_value_string_utf8(env, args[0], buffer, bufsize, &resultSize);
    if (status != napi_ok) {
        napi_throw_type_error(env, nullptr, "Argument must be a string");
        return nullptr;
    }
    std::string value(buffer, resultSize);
    Base64Decoder decoder;

    string decoded = decoder.decode(value);
    napi_value result;
    napi_create_string_utf8(env, decoded.c_str(), decoded.length(), &result);
    return result;
}
static napi_value NAPI_Global_encode(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (argc < 1) {
        napi_throw_type_error(env, nullptr, "Expected one argument");
        return nullptr;
    }
    size_t bufsize = 1024;
    char buffer[1024];
    size_t resultSize;
    napi_status status = napi_get_value_string_utf8(env, args[0], buffer, bufsize, &resultSize);
    if (status != napi_ok) {
        napi_throw_type_error(env, nullptr, "Argument must be a string");
        return nullptr;
    }
    string value(buffer, resultSize);
    string encode = Base64Encoder::encode(value);
    napi_value result;
    napi_create_string_utf8(env, encode.c_str(), encode.length(), &result);
    return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"isStringEmpty", nullptr, NAPI_String_isEmpty, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"decoder", nullptr, NAPI_Global_decoder, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"encode", nullptr, NAPI_Global_encode, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "library",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
